package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.dto.UserInfoOutputDTO;
import com.partners.dao.dto.UserInputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.UserService;
import com.partners.utils.ApiResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Map;

/**
 * 用户管理
 * @author liangyongle
 * @date 2018-12-18
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;



    /**
     * 获取用户个人信息
     * @Param userDO UserDO
     * return Integer
     * */
    @PostMapping("user_info")
    public ApiResult getUserInfo(@SimpleParam("id") Integer id){
        UserInfoOutputDTO detail = userService.getUserInfo(id);
        return ApiResultUtil.ok(detail);
    }


    /**
     * 编辑用户
     * @Param userDO UserDO
     * return Integer
     * */
    @PostMapping("editUser")
    public ApiResult editUser(@RequestBody UserInputDTO userInputDTO){
        if(userInputDTO.getName()  == null){
            throw BusinessException.of(ErrorMessageEnum.NAME_NOT_FOUND);
        }else if(userInputDTO.getPhone()  == null ){
            throw BusinessException.of(ErrorMessageEnum.PHONE_NOT_FOUND);
        }
        Integer result = userService.editUser(userInputDTO);
        return ApiResultUtil.ok(result);
    }

    /**
     * 获取所有的用户列表
     *@Param paging Paging
     *@return list
     * */
    @PostMapping("getAllUser")
    public ApiResult getAllUserList(@BeanValid @RequestBody Paging paging,
                                    @SimpleParam("returnType") Integer returnType){
        returnType = returnType != null?returnType:0;
        Map<String,Object> list = userService.getAllUserList(paging,returnType);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取旗下用户列表
     * @Param paging Paging uid Integer
     * return List
     * */
    @PostMapping("getUserListByUid")
    public ApiResult getUserListByUid(@BeanValid @RequestBody Paging paging,
                                      @SimpleParam("uid") Integer uid,
                                      @SimpleParam("returnType") Integer returnType){
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        returnType = returnType != null?returnType:0;
        Map<String,Object> list = userService.getUserListByUid(paging,uid,returnType);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取合伙人详情
     * @Param id Integer
     * return Object
     * */
    @PostMapping("getUserDeail")
    public ApiResult<UserInfoOutputDTO> getUserDetail(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        UserInfoOutputDTO detail = userService.getuserDetailById(id);
        return ApiResultUtil.ok(detail);
    }


    /**
     * 报备管理
     * @Param id Integer
     * return Object
     * */
     @PostMapping("all_user")
     public ApiResult getAllUserList(@BeanValid @RequestBody Paging paging,
                                     @SimpleParam("name") String name,
                                     @SimpleParam("phone") BigInteger phone){
        Map<String,Object> list = userService.getAllCustomerList(paging,name,phone);
       return ApiResultUtil.ok(list);
     }

    /**
     * 删除用户
     * @Param id Integer
     * return boolean
     * */
    @PostMapping("delete_user")
    public ApiResult deleteUser(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        userService.deleteUser(id);
        return ApiResultUtil.ok(SuccessMessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 控制用户积分是否显示
     * @Param uid Integer show Integer
     * return boolean
     * */
    @PostMapping("point_show")
    public ApiResult changePointShow(@SimpleParam("uid") Integer uid,@SimpleParam("show") Integer show){
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        show = show != null?show:0;
        userService.changePointShow(uid,show);
        return ApiResultUtil.ok(SuccessMessageEnum.OPERATION_SUCCESS);
    }


 }
